package probe.logic;

import java.util.ArrayList;

public class Delete {

	private int _deleteType;
	private ArrayList<Task> _deletedList;
	private ArrayList<Task> _deletedTask;
	private String _deleteDetails;
	
	private static final int BY_STATUS = 1;
	private static final int BY_DATE = 2;
	private static final int BY_TASKNAME = 3;
	private static final int ALL = 4;
	
	private static final String STATUS_DONE = "DONE";
	private static final String STATUS_UNFINISHED = "UNFINISHED";
	private static final String DELETE_ALL = "ALL";
	
		
	public Delete() {
		_deletedList = new ArrayList<Task> ();
		_deletedTask = new ArrayList<Task> ();
	}
	
	public void verifyDeleteType() {
		
		if(_deleteDetails.equals(DELETE_ALL)) {
			_deleteType = ALL;
		}
		if(_deleteDetails.equals(STATUS_DONE) || _deleteDetails.equals(STATUS_UNFINISHED)) {
			_deleteType = BY_STATUS;
		} else {
			try {
				Date date = new Date(_deleteDetails);
				if(date.isValidDate())
					_deleteType = BY_DATE;
				else
					_deleteType = BY_TASKNAME;
			} catch (NumberFormatException e) 
			{		
				_deleteType = BY_TASKNAME;
			}
		}
	}
	
	public void doSearch() {
		if(_deleteType == ALL) {
			deleteAll();
		}
		if(_deleteType == BY_STATUS) {
			searchByStatus();
		}
		if(_deleteType == BY_DATE) {
			searchByDate();
		}
		if(_deleteType == BY_TASKNAME) {
			searchByTaskName();
		}
	}
	
	private void deleteAll() {
		_deletedTask = _deletedList;
		_deletedList = new ArrayList<Task> (); 
	}

	private void searchByStatus() {
		
		if(_deleteDetails.equals(STATUS_UNFINISHED)) {
			for(int i = 0; i < _deletedList.size(); i++) {
				Task task = _deletedList.get(i);
				if(task.taskStatus == Task.TaskStatus.UNFINISHED) {
					_deletedTask.add(task);
					_deletedList.remove(task);
					i--;
				}
			}
		} else {
			for(int i = 0; i < _deletedList.size(); i++) {
				Task task = _deletedList.get(i);
				if(task.taskStatus == Task.TaskStatus.DONE) {
					_deletedTask.add(task);
					_deletedList.remove(task);
					i--;
				}
			}
		}
	}

	private void searchByDate() {
		
		Date date = new Date(_deleteDetails);
		for(int i = 0; i < _deletedList.size(); i++) {
			Task task = _deletedList.get(i);
			if(task.taskType == Task.TaskType.DEADLINE_TASK) {
				if(task.deadline.isSameDate(date)) {
					_deletedTask.add(task);
					_deletedList.remove(task);
					i--;
				}
			}
			if(task.taskType == Task.TaskType.STET_TASK) {
				if(task.startingDate.isSameDate(date)) {
					_deletedTask.add(task);
					_deletedList.remove(task);
					i--;
				}
				else if(task.endingDate.isSameDate(date)) {
					_deletedTask.add(task);
					_deletedList.remove(task);
					i--;
				}
			}
		}
		
	}
	
	private void searchByTaskName() {
		
		for(int i = 0; i < _deletedList.size(); i++) {
			Task task = _deletedList.get(i);
			if(task.taskName.equalsIgnoreCase(_deleteDetails)) {
				_deletedTask.add(task);
				_deletedList.remove(i);
				i--;
			}
		}
	}
	
	public ArrayList<Task> process (ArrayList<Task> list, String command) {
		
		_deletedList = list;
		_deleteDetails = command;
		_deletedTask = new ArrayList<Task> ();
		
		verifyDeleteType();

		doSearch();
		
		return _deletedList;
	}
	
	public ArrayList<Task> getDeletedTasks() {
		return _deletedTask;
	}
}
